package com.jl.bean;

import com.util.EncodingUtil;

public class FF {

	private String STX = "FFFF";
	private String RSCTL = "";
	private String COMMAND = "";
	private String DATE = "";
	private String BCC = "";
	private String ETX = "FF";

	public String send(String rsctl, String command, String date) {
		this.RSCTL = rsctl;
		this.COMMAND = EncodingUtil.desToHex(Integer.parseInt(command), 2);
		this.DATE = date;
		// 异或校验从RSCTL到DATA所有字节的异或值
		String data = RSCTL + COMMAND + DATE;
		// 异或校验从RSCTL到DATA所有字节的异或值
		BCC = EncodingUtil.yihuo(EncodingUtil.change(data));
		// 拼接完整的请求  // 转义FF - FE01 FE-FE00
		String retn = STX + convertFF(RSCTL + COMMAND + DATE + BCC) + ETX;
		return retn.toUpperCase();
	}
	/**
	 * 转换FF协议
	 * @param str
	 * @return
	 */
	public static String convertFF(String str) {
		String context = "";
		String [] strs = EncodingUtil.change(str).split(" ");
		for (int i = 0; i < strs.length; i++) {
			if(strs[i].equals("FE")){
				strs[i] = "FE00";
			}
		}
		for (int i = 0; i < strs.length; i++) {
			if(strs[i].equals("FF")){
				strs[i] = "FE01";
			}
		}

		for (int i = 0; i < strs.length; i++) {
			context+=strs[i];
		}

		return context;
	}

}
